//
//  DishDetailView.m
//  XRmenu
//
//  Created by gaomz on 11-3-24.
//  Copyright 2011 E0571. All rights reserved.
//

#import "DishDetailView.h"
#import "XRmenuHelper.h"

@implementation DishDetailView
@synthesize deleagate;

-(id)initWithStyle:(NSString *)stylename DishSubViewName:(NSString *)vname Product:(Product *)prd
{
	NSDictionary* aDic = [self findNode:APPSESSION.allconfig withKey:vname];
	if ((self=[super initWithConfig:aDic])) {
		[self add1fingerLeftSwipe];
		[self add1fingerRightSwipe];
		[self add1fingerTopSwipe];
		[self add1fingerDownSwipe];
		
		pr = [prd retain];
		
		UILabel *lbTitle = (UILabel *)[self viewWithTag:kTagOfStartLabel];
		[lbTitle setText:[XRmenuPublic jsonfield:pr.pName]];
		
		UILabel *lbPrice = (UILabel *)[self viewWithTag:(kTagOfStartLabel + 1)];
		[lbPrice setText:[NSString stringWithFormat:@"¥ %d",[pr.pSalePrice intValue] ]];
		
		UILabel *lbComment = (UILabel *)[self viewWithTag:(kTagOfStartLabel + 2)];
		[lbComment setNumberOfLines:0];
		
		XRmenuError *error=nil;
		arrFile = [[XRmenuPublic getDishPhotoDetailesImageFilesName:pr.pEntityId Error:&error] copy];
		
		
		
		UIImageView *photoImage = (UIImageView *)[self viewWithTag:(kTagOfStartImgV+1)];
		
		CGRect imgFrame = photoImage.frame;
		
		CGRect pageControlFrame = CGRectMake(imgFrame.origin.x, imgFrame.origin.y+imgFrame.size.height-40.0, imgFrame.size.width, 40.0);
		
		pageControl =[[UIPageControl alloc] initWithFrame:pageControlFrame];
		//[pageControl setBackgroundColor:[UIColor blackColor]];
		//[pageControl addTarget:self action:@selector(pageIndexChange:) forControlEvents:UIControlEventValueChanged];
		pageControl.numberOfPages= [arrFile count];
		
		if ([arrFile count]>0) {
			[self addSubview:pageControl];
			pageControl.currentPage=0;
			[self setcurrentPageIndex:0];
		}
		else {
			
		}

		
	
		
		
		UIButton *btn  = (UIButton *)[self viewWithTag:(kTagOfStartBT+1)];
		[self detailButton_Clicked:btn];
		
	}
	return self;
}

#pragma mark pageControl 操作
-(void)pageIndexChange:(UIPageControl*)aPageControl
{
	//DebugLog(@"changed:%D",aPageControl.currentPage);	
	[self changeDetailPhoto:aPageControl.currentPage];
}

-(void)changeDetailPhoto:(NSInteger)photoNo
{
	UIImageView *photoImage = (UIImageView *)[self viewWithTag:(kTagOfStartImgV+1)];
	UIImage *img = [UIImage imageWithContentsOfFile:[arrFile objectAtIndex:photoNo]];
	[photoImage setImage:img];
	
}

-(void) setcurrentPageIndex:(NSInteger)aIndex
{
	pageControl.currentPage=aIndex;
	[self changeDetailPhoto:aIndex];
}

#pragma mark 按钮点击事件
-(void) buttonClicked:(id)sender
{
	
	
	NSInteger tag=[sender tag];
	switch (tag) {
		case kTagOfStartBT:
			[self removeFromSuperview];
			break;
		case (kTagOfStartBT +4):
			[deleagate AddDish];
			break;
		default:
			[self detailButton_Clicked:sender];
			
			break;
	}
}

-(void)detailButton_Clicked:(id)sender
{
	NSInteger tag = [sender tag];
	UIButton *btn = (UIButton *)sender;

	
	for (int i=1; i<4; i++) {
		btn=(UIButton *)[self viewWithTag:(kTagOfStartBT+i)];
		if (tag==kTagOfStartBT+i) {
			[btn setEnabled:NO];
		}
		else {
			[btn setEnabled:YES];
		}
		
	}
	[self showDetailContent:tag];
}

-(void)showDetailContent:(NSInteger)tag
{
	UILabel *lbComment = (UILabel *)[self viewWithTag:(kTagOfStartLabel + 2)];
	switch (tag) {
		case (kTagOfStartBT+1):			
			[lbComment setText:[XRmenuHelper getCurrentEx:pr].dBriefEx];
			break;
		case (kTagOfStartBT+2):
			[lbComment setText:[XRmenuHelper getCurrentEx:pr].dRecipesEx];
			break;
		default:
			[lbComment setText:@""];
			break;
			break;
	}
}





#pragma mark 手势操作
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
	NSInteger totalPages = pageControl.numberOfPages;
	NSInteger currentPage = pageControl.currentPage;
	if (currentPage < (totalPages-1) ) {
		currentPage++;
		[self setcurrentPageIndex:currentPage];
	}
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
	NSInteger totalPages = pageControl.numberOfPages;
	if (totalPages>0) {
		NSInteger currentPage = pageControl.currentPage;
		if (currentPage >0 ) {
			currentPage--;
			[self setcurrentPageIndex:currentPage];
		}
		
	}
}

-(void)handleSwipeTop:(UISwipeGestureRecognizer *)recognizer
{
	
}
-(void)handleSwipeDown:(UISwipeGestureRecognizer *)recognizer
{

}


#pragma mark 语言切换
-(void) changeLanguage:(NSNotification *)notification
{
	// todo: 切换语言
	NSDictionary *dict = [APPSESSION.allconfig findAtPath:[NSString stringWithFormat:@"UI Skin/%@/Dish Main View/Dish Photo View/Dish Detail View", APPSESSION.uistyle]];
	if (dict) {
		NSArray *bts = (NSArray*)[dict objectForKey:@"button list"];
		[self translateButtons:bts];
	}
	
}

-(void)dealloc
{
	[pr release];
	[arrFile release];
	[normalImage release];
	[selectedImage release];
	[pageControl release];
	[super dealloc];
}

@end
